package eazy.ListNode;

import utils.ListNode;

public class reverseList_206 {
    public static void main(String[] args) {
        reverseList_206 reverseList = new reverseList_206();
        ListNode listNode = new ListNode();
        ListNode listNode2 = new ListNode();
        listNode.val=1;
        listNode.next=listNode2;
        listNode2.val=2;
        System.out.println(listNode.val);
        System.out.println(listNode.next.val);
        ListNode listNode1 = reverseList.reverseList_iteration(listNode);
        System.out.println(listNode1.val);
        System.out.println(listNode1.next.val);

    }
    public ListNode reverseList_iteration(ListNode head) {
        ListNode prev = null;
        ListNode curr = head;
        while (curr != null) {
            ListNode nextTemp = curr.next;
            curr.next = prev;
            prev = curr;
            curr = nextTemp;
            //暂存第二个数据
            //头节点指向空
            //  pre指向头节点
            //  curr指向第二个节点
        }
        return prev;
    }
    public ListNode reverseList_recursion(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode p = reverseList_recursion(head.next);
        head.next.next = head;
        head.next = null;
        return p;
        //一直递归到链表的最后一个结点
        //让当前结点的下一个结点的 next指针指向当前节点
        //让当前结点的 nextnext 指针指向 NULLNULL
    }
}
